package ysq.xyg.recovery.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.extend.ehcache.EhCacheService;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.recovery.dao.RecoveryMiddleCatDao;
import ysq.xyg.recovery.model.po.RecoveryMiddleCat;
import ysq.xyg.recovery.model.vo.RecoveryMiddleCatVo;
import ysq.xyg.recovery.service.RecoveryMiddleCatService;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.sys.model.po.SysUser;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 回收金模块-中分类service实现
 *
 * @author congcong tao
 * @date 2019-5-8 15:12:13
 */
@Service("recoveryMiddleCatService")
public class RecoveryMiddleCatServiceImpl extends BaseServiceImpl<RecoveryMiddleCat> implements RecoveryMiddleCatService {

    @Autowired
    private EhCacheService ehCacheService;
    @Resource
    private RecoveryMiddleCatDao recoveryMiddleCatDao;

    private RecoveryMiddleCat recoveryMiddleCat;

    @Autowired
    public void setBaseDao(RecoveryMiddleCatDao recoveryMiddleCatDao) {
        this.baseDao = recoveryMiddleCatDao;
    }

    /**
     * 分页查询
     *
     * @param recoveryMiddleCatVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-8 15:12:13
     */
    @Override
    public ReturnEntity page(RecoveryMiddleCatVo recoveryMiddleCatVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<RecoveryMiddleCat> list = acceptList(recoveryMiddleCatVo);
        PageInfo<RecoveryMiddleCat> page=new PageInfo<RecoveryMiddleCat>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param recoveryMiddleCatVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-8 15:12:13
     */
    @Override
    public ReturnEntity addOrUpdate(RecoveryMiddleCatVo recoveryMiddleCatVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(recoveryMiddleCatVo.getId())){
            recoveryMiddleCat=new RecoveryMiddleCat(currentUser);
            isUpate=false;
        }else{
            recoveryMiddleCat=selectByPrimaryKey(recoveryMiddleCatVo.getId());
            if(ObjectUtil.isNull(recoveryMiddleCat)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            recoveryMiddleCat.setUpdateTime(new Date());
            recoveryMiddleCat.setUpdateUser(currentUser.getId());
        }
        recoveryMiddleCat.setLargeCatId(recoveryMiddleCatVo.getLargeCatId());
        recoveryMiddleCat.setName(recoveryMiddleCatVo.getName());
        recoveryMiddleCat.setIcon(recoveryMiddleCatVo.getIcon());
        recoveryMiddleCat.setDescription(recoveryMiddleCatVo.getDescription());
        recoveryMiddleCat.setPrice(recoveryMiddleCatVo.getPrice());
        recoveryMiddleCat.setRatePrice(recoveryMiddleCatVo.getRatePrice());
        recoveryMiddleCat.setOrderNum(recoveryMiddleCatVo.getOrderNum());
        recoveryMiddleCat.setStatus(recoveryMiddleCatVo.getStatus());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(recoveryMiddleCat);
        }else{
            num=insert(recoveryMiddleCat);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        // 更新缓存
        ehCacheService.evictRecoveryCat();
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-8 15:12:13
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        RecoveryMiddleCat recoveryMiddleCat=selectByPrimaryKey(id);
        if(recoveryMiddleCat!=null){
            recoveryMiddleCat.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            recoveryMiddleCat.setUpdateTime(new Date());
            recoveryMiddleCat.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(recoveryMiddleCat);
            if(num>0){
                // 更新缓存
                ehCacheService.evictRecoveryCat();
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param recoveryMiddleCatVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-5-8 15:12:13
     */
    @Override
    public List<RecoveryMiddleCat> acceptList(RecoveryMiddleCatVo recoveryMiddleCatVo){
        Example example = new Example(RecoveryMiddleCat.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(recoveryMiddleCatVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",recoveryMiddleCatVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(recoveryMiddleCatVo.getLargeCatId())){
            criteria.andEqualTo("largeCatId",recoveryMiddleCatVo.getLargeCatId());
        }
        if(StringUtil.isNotEmpty(recoveryMiddleCatVo.getName())){
            criteria.andLike("name","%" + recoveryMiddleCatVo.getName() + "%");
        }

        example.setOrderByClause(" update_time desc");
        return recoveryMiddleCatDao.selectByExample(example);
    }

    @Override
    public ReturnEntity acceptSelect(String code) {
        List<RecoveryMiddleCat> list = acceptList(new RecoveryMiddleCatVo());
        List<SysDictContent> sysDictContentList=new ArrayList<SysDictContent>();
        for(RecoveryMiddleCat entity : list){
            SysDictContent sysDictContent=new SysDictContent();
            sysDictContent.setId(entity.getId());
            sysDictContent.setName(entity.getName());
            sysDictContent.setDictType(code);
            sysDictContent.setValue(entity.getId());
            sysDictContentList.add(sysDictContent);
        }
        return new ReturnEntity(sysDictContentList);
    }
}
